<template>
  <div class="app-container" v-loading="pageLoading">
    <div class="filter-container">
      <el-input style="width: 360px" placeholder="输入任务号/托盘号/目标库位等查询" clearable prefix-icon="el-icon-search"
        class="filter-item" v-model="query.queryString" @change="research"></el-input>
      <el-button class="filter-item" type="success" plain @click="research">查询</el-button>
    </div>
    <z-table ref="ztable" :list="list" :tableProps="tableProps" :columns="columns">
      <template v-slot:content="v">
        <template v-if="v.key === 'TaskCode'">
          <a href="#" :class="{ pointer: v.row.TaskDetailId, 'text-primary': v.row.TaskDetailId }"
            @click.prevent="seeTaskDetail(v.row)">{{ v.row[v.key] }}</a>
        </template>
        <span v-else-if="v.key === 'StatusTxt'">
          <el-tag :type="v.row.statusTagType">{{ v.row[v.key] }}</el-tag>
        </span>
        <span v-else-if="v.key === 'TypeTxt'">
          <el-tag class="text-label" :class="v.row.typeClass">{{
            v.row[v.key]
          }}</el-tag>
        </span>
        <span v-else-if="v.key === 'PriorityTxt'">
          <el-tag v-if="!!v.row[v.key]" :type="v.row.priorityTagType">{{
            v.row[v.key]
          }}</el-tag>
        </span>
        <span v-else>{{ v.row[v.key] }}</span>
      </template>
      <template v-slot:operation="v">
        <template v-if="!v.row.taskDetalisId">
          <a href="#" class="text-yellow" :data-disabled="checkBtnIsOnclick(v.row.status, true)"
            :data-loading="SuspendMainTaskLoading && opeartionIndex == v.$index"
            @click.prevent="doMainTask(v.row, 'SuspendMainTask', v.$index)">强制暂停</a>
          <a href="#" class="text-green" :data-disabled="checkBtnIsOnclick(v.row.status, false)"
            :data-loading="opeartionIndex && opeartionIndex == v.$index"
            @click.prevent="doMainTask(v.row, 'ForceFinishMainTask', v.$index)">强制完成</a>
          <a href="#" class="text-red" :data-disabled="checkBtnIsOnclick(v.row.status, false)"
            :data-loading="CancelMainTaskLoading && opeartionIndex == v.$index"
            @click.prevent="doMainTask(v.row, 'CancelMainTask', v.$index)">取消任务</a>
        </template>
        <template v-else>
          <a href="#" class="text-yellow" :data-disabled="checkBtnIsOnclick(v.row.status, true, true)"
            :data-loading="SuspendTaskDetLoading && opeartionIndex == v.$index"
            @click.prevent="doDetailTask(v.row, 'SuspendTaskDet', v.$index)">暂停</a>
          <a href="#" class="text-blue" :data-disabled="checkBtnIsOnclick(v.row.status, false, true)"
            :data-loading="ContExecTaskDetLoading && opeartionIndex == v.$index"
            @click.prevent="doDetailTask(v.row, 'ContExecTaskDet', v.$index)">继续</a>
          <a href="#" class="text-green" :data-disabled="checkBtnIsOnclick(v.row.status, false, true)" :data-loading="ForceFinishTaskDetLoading && opeartionIndex == v.$index
            " @click.prevent="doDetailTask(v.row, 'ForceFinishTaskDet', v.$index)">强制执行</a>
          <a href="#" class="text-red" :data-disabled="checkBtnIsOnclick(v.row.status, false, true)"
            :data-loading="CancelTaskDetLoading && opeartionIndex == v.$index"
            @click.prevent="doDetailTask(v.row, 'CancelTaskDet', v.$index)">取消</a>
        </template>
      </template>
    </z-table>

    <z-pagination :pagination="pagination" :total="total" v-model:page="query.page" v-model:limit="query.size"
      @change="getList"></z-pagination>
    <!-- 子任务明细 -->
    <archive-detail-dialog v-model:visible="detailVisible" :query="driverDetailQuery" :isAutoRequst="false"
      :controlBtnDisabledShow="true" @statusSuccesCallBack="research"></archive-detail-dialog>
  </div>
</template>

<script>
import commonMixin from "@/views/_common/mixin";
import archiveDetailDialog from "./components/archiveDetailDialog.vue";
import _ from 'lodash'
export default {
  mixins: [commonMixin],
  components: { archiveDetailDialog },
  name: "wcsArchiveTask",
  data: function () {
    return {
      api: this.$api.wcsUrl + "wcs/task/",
      apiList: this.$api.wcsUrl + "wcs/task/GetTaskMainFileList",
      postForList: true,
      columns: [
        { title: "WCS任务编号", key: "TaskCode", width: 160, fixed: true },
        { title: "任务来源", key: "TaskSource", width: 90, fixed: true },
        { title: "任务类型", key: "TaskType", width: 90, fixed: true },
        { title: "任务状态", key: "Status", width: 90 },
        { title: "优先级", key: "Priority", width: 68 },
        { title: "开始位置", key: "StartLocationUserCode", width: 90 },
        { title: '开始设备', key: 'StartLocationDeviceName', width: 120 },
        { title: "结束位置", key: "EndLocationUserCode", width: 90 },
        // { title: "当前位置", key: "CurrentLocationUserCode", width: 90 },
        // 
        { title: '结束设备', key: 'EndLocationDeviceName', width: 120 },
        { title: "托盘号", key: "Barcode", width: 140 },
        { title: "任务创建时间", key: "CreatedAt", width: 160 },
        { title: "信息", key: "Description" },
        { title: "来源任务码", key: "InterCode", width: 120 },
        { title: "开始时间", key: "CreatedAt", width: 160 },
        { title: "完成时间", key: "FinishedAt", width: 160 },
      ],
      tableProps: {
        border: true,
        opsColWith: 180,
        hideOperations: true,
        lazy: true,
        load: this.showTaskDetail,
        "row-key": 'Id',
        "tree-props": { children: "children", hasChildren: "hasChildren" },
      },
      typeOptions: [
        { label: "未知", value: 0, class: "bg-gray" },
        { label: "出库", value: 1, class: "bg-blue" },
        { label: "入库", value: 2, class: "bg-green" },
        { label: "移库", value: 3, class: "bg-yellow" },
        { label: "agv入库", value: 4, class: "bg-green" },
        // { label: "跨库", value: 5, class: "bg-yellow" },
      ],
      statusOptions: [
        { label: "新任务", value: 0, tagType: "primary" },
        { label: "已发送", value: 1, tagType: "success" },
        { label: "正在运行", value: 2, tagType: "success" },
        { label: "暂停", value: 3, tagType: "warning" },
        { label: "发生错误", value: 4, tagType: "danger" },
        { label: "被取消", value: 5, tagType: "info" },
        { label: "已完成", value: 6, tagType: "success" },
        { label: "强制暂停", value: 9, tagType: "warning" },
      ],
      sourceOptions: [
        { label: "未知", value: 0 },
        { label: "WCS", value: 1 },
        { label: "WMS", value: 2 },
      ],
      priorityOptions: [
        { label: "暂缓", value: -1, tagType: "info" },
        { label: "一般", value: 0, tagType: "primary" },
        { label: "高", value: 5, tagType: "warning" },
        { label: "特高", value: 10, tagType: "danger" },
      ],
      lastExpandItem: null,
      detailVisible: false,
      driverDetailQuery: "",

      query: {
        size: 10,
        page: 1,
        queryString: "",
      },
    };
  },
  methods: {

    // 查看子任务详情
    seeTaskDetail(row) {
      if (!row.TaskDetailId) return;
      this.driverDetailQuery = row.TaskDetailId;
      this.detailVisible = true;
    },
    formatList(list) {
      let tmp = _.cloneDeep(list);
      let forList = tmp.map((i) => {
        // 当前时间戳


        i.Id = Math.random().toString(36).substring(2, 14) + new Date().getTime(); // 随机数暂时解决Row-key问题
        i.TaskSource = this.sourceOptions.find(
          (x) => x.value == i.TaskSource
        )?.label;
        i.TaskType = this.typeOptions.find((x) => x.value == i.TaskType)?.label;
        i.typeClass = this.typeOptions.find(
          (x) => x.value == i.TaskType
        )?.class;
        i.Status = this.statusOptions.find(
          (x) => x.value == i.Status
        )?.label;
        i.statusTagType = this.statusOptions.find(
          (x) => x.value == i.Status
        )?.tagType;
        i.Priority = this.priorityOptions.find(
          (x) => x.value == i.Priority
        )?.label;
        i.priorityTagType = this.priorityOptions.find(
          (x) => x.value == i.Priority
        )?.tagType;
        if (!i.TaskDetailId) i.hasChildren = true;
        return i;
      });
      return forList
    },
    getDictionary() {
      this.http.post('api/Sys_Dictionary/GetVueDictionary', [
        "WMS_TaskType",

      ]).then(r => {
        this.typeOptions = r.find(i => i.DictionaryNo == "WMS_TaskType")?.data.map(i => {
          return {
            label: i.value,
            value: i.key,
          }
        })
      })
    },
    showTaskDetail(item, node, resolve) {
      if (item.TaskDetailId) return resolve([])
      this.http
        .get(this.api + "GetTaskDetalisFile", {
          taskCode: item.TaskCode,
        })
        .then((r) => {
          // item.children = this.formatList(r)
          // resolve([]);
          resolve(this.formatList(r));
        });
    },
    editPriority(task) {
      this.data = _.cloneDeep(task);
      this.editFormVisible = true;
    },
    refreshChildren() {
      if (this.lastExpandItem) {
        let { item, node, resolve } = this.lastExpandItem;
        this.showTaskDetail(item, node, resolve);
      }
    },
  },
  created: function () {
    this.getDictionary()
    this.getList();
  },
};
</script>
<style scoped>
.task-code {
  cursor: pointer;
  font-weight: 650;
  color: #7300e6;
}
</style>
